package com.he.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.he.annotation.OperationLogger;
import com.he.constant.CommonConstant;
import com.he.domain.Result;
import com.he.domain.dto.AlbumDTO;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.PageResult;
import com.he.domain.vo.AlbumBackVO;
import com.he.domain.vo.AlbumVO;
import com.he.entity.Album;
import com.he.entity.BlogFile;
import com.he.enums.FilePathEnum;
import com.he.mapper.AlbumMapper;
import com.he.mapper.BlogFileMapper;
import com.he.service.AlbumService;
import com.he.strategy.context.UploadStrategyContext;
import com.he.utils.BeanCopyUtils;
import com.he.utils.PageUtils;
import jakarta.annotation.Resource;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/16 11:10
 * @Desc 相册Service实现类
 */
@Service
public class AlbumServiceImpl  extends ServiceImpl<AlbumMapper, Album> implements AlbumService {

    @Resource
    private AlbumMapper albumMapper;

    @Resource
    private BlogFileMapper blogFileMapper;

    /**
     * 上传策略上下文
     */
    @Resource
    private UploadStrategyContext uploadStrategyContext;

    @Override
    public List<AlbumVO> listAlbumVO() {
        return albumMapper.selectAlbumVOList();
    }

    @Override
    public PageResult<AlbumBackVO> listAlbumBackVO(ConditionDTO condition) {
        // 查询相册数量
        Long count = albumMapper.selectCount(new LambdaQueryWrapper<Album>()
                .like(StringUtils.hasText(condition.getKeyword()), Album::getAlbumName, condition.getKeyword()));
        if (count == 0) {
            return new PageResult<>();
        }
        // 查询相册信息
        List<AlbumBackVO> albumList = albumMapper.selectAlbumBackVO(PageUtils.getLimit(), PageUtils.getSize(), condition.getKeyword());
        return new PageResult<>(albumList, count);
    }

    @Override
    public void addAlbum(AlbumDTO album) {
        // 相册是否存在
        Album existAlbum = albumMapper.selectOne(new LambdaQueryWrapper<Album>()
                .select(Album::getId)
                .eq(Album::getAlbumName, album.getAlbumName()));
        Assert.isNull(existAlbum, album.getAlbumName() + "相册已存在");
        // 添加新相册
        Album newAlbum = BeanCopyUtils.copyBean(album, Album.class);
        baseMapper.insert(newAlbum);
    }

    @Override
    public void deleteAlbum(Integer albumId) {
        // 相册是否存在
        Album existAlbum = albumMapper.selectOne(new LambdaQueryWrapper<Album>()
                .select(Album::getId)
                .eq(Album::getId, albumId));
        Assert.notNull(existAlbum, "相册不存在");
        // 删除相册
        baseMapper.deleteById(albumId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String uploadAlbumCover(MultipartFile file) {
        String url = uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.ALBUM.getPath());
        String baseName = FilenameUtils.getBaseName(url);
        // 后缀
        String extension = FilenameUtils.getExtension(url);
        BlogFile blogFile = BlogFile.builder()
                .fileUrl(url)
                .filePath(FilePathEnum.ARTICLE.getFilePath())
                .fileName(baseName)
                .extendName(extension)
                .fileSize((int) file.getSize())
                .isDir(CommonConstant.FALSE)
                .build();
        blogFileMapper.insert(blogFile);
        return url;
    }

    @Override
    public AlbumDTO editAlbum(Integer albumId) {
        return albumMapper.selectAlbumById(albumId);
    }

    @Override
    public void updateAlbum(AlbumDTO album) {
        // 相册是否存在
        Album existAlbum = albumMapper.selectOne(new LambdaQueryWrapper<Album>()
                .select(Album::getId)
                .eq(Album::getAlbumName, album.getAlbumName()));
        Assert.isFalse(Objects.nonNull(existAlbum) && !existAlbum.getId().equals(album.getId()),
                album.getAlbumName() + "相册已存在");
        // 修改相册
        Album newAlbum = BeanCopyUtils.copyBean(album, Album.class);
        baseMapper.updateById(newAlbum);
    }


}
